package com.ziyia.wim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ziyia.wim.util.HttpUtil;
import com.ziyia.wim.R;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
;

public class LoginActivity extends BaseActivity {

    private Button login;
    private TextView register;
    private EditText username, password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);


        register.setOnClickListener( v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

        login.setOnClickListener(v -> {
            String name = this.username.getText().toString();
            String pass = this.password.getText().toString();


            if ("".equals(name) || "".equals(pass)) {
                Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }



            String url = "http://" + IP + "/user/login/?username=" + name + "&password=" + pass;
            HttpUtil.get(url, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String resp = response.body().string();

                    JSONObject jsonObject = JSON.parseObject(resp);

                    Map<String, Object> innerMap = jsonObject.getInnerMap();

                    Integer code = (Integer) innerMap.get("code");

                    Looper.prepare();
                    if (0 == code) {
                        Map msg = jsonObject.getObject("msg", Map.class);
                        Log.d("msg", msg.toString());
                        Intent intent = new Intent(LoginActivity.this, Index.class);
                        intent.putExtra("uid", Objects.requireNonNull(msg.get("id")).toString());
                        intent.putExtra("username", Objects.requireNonNull(msg.get("username")).toString());
                        startActivity(intent);
                        LoginActivity.this.finish();
                    } else {
                        String msg = (String) innerMap.get("msg");
                        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        });
    }
}